﻿using System;
using Newtonsoft.Json.Serialization;

namespace TypeScriptJson.Generator
{
	public class ArrayTypeMapping: TypeMapping
	{
		public JsonArrayContract Contract { get; private set; }
		public TypeMapping ArrayElementMapping { get; set; }

		public override bool IsGenerateDeclaration
		{
			get { return false; }
		}

		public override string GetTypeNameInternal()
		{
			return "Array$" + ArrayElementMapping.TypeName + "$";
		}

		public ArrayTypeMapping(JsonArrayContract contract) : base(contract.UnderlyingType)
		{
			Contract = contract;
		}

		protected override void ProcessInternal(GeneratorContext context)
		{
			ArrayElementMapping = context.MapType(Contract.CollectionItemType);
		}

		public override void WriteTypeReference(TypeScriptCodeWriter codeWriter, string currentModule)
		{
			if (CustomTypeName != null)
				base.WriteTypeReference(codeWriter, currentModule);
			else
			{
				ArrayElementMapping.WriteTypeReference(codeWriter, currentModule);
				codeWriter.Writer.Write("[]");
			}
		}

		public override void WriteTypeDeclaration(TypeScriptCodeWriter codeWriter, GeneratorContext context)
		{
			throw new InvalidOperationException();
		}
	}
}